// 日期格式化
export function parseTime(time, pattern) {
	if (arguments.length === 0 || !time) {
	  return null;
	}
	const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
	let date;
	if (typeof time === "object") {
	  date = time;
	} else {
	  if (typeof time === "string" && /^[0-9]+$/.test(time)) {
		time = parseInt(time);
	  } else if (typeof time === "string") {
		time = time.replace(new RegExp(/-/gm), "/");
	  }
	  if (typeof time === "number" && time.toString().length === 10) {
		time = time * 1000;
	  }
	  date = new Date(time);
	}
	const formatObj = {
	  y: date.getFullYear(),
	  m: date.getMonth() + 1,
	  d: date.getDate(),
	  h: date.getHours(),
	  i: date.getMinutes(),
	  s: date.getSeconds(),
	  a: date.getDay(),
	};
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
	  let value = formatObj[key];
	  // Note: getDay() returns 0 on Sunday
	  if (key === "a") {
		return ["日", "一", "二", "三", "四", "五", "六"][value];
	  }
	  if (result.length > 0 && value < 10) {
		value = "0" + value;
	  }
	  return value || 0;
	});
	return time_str;
  }
  
  //获取本周、上周、下周的起始、结束日期
  export function getMonday(type, dates) {
	var now = new Date();
	var nowTime = now.getTime();
	var day = now.getDay();
	var longTime = 24 * 60 * 60 * 1000;
	var n = longTime * 7 * (dates || 0);
	if (type == "s") {
	  var dd = nowTime - (day - 1) * longTime + n;
	}
	if (type == "e") {
	  var dd = nowTime + (7 - day) * longTime + n;
	}
	dd = new Date(dd);
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1;
	var d = dd.getDate();
	m = m < 10 ? "0" + m : m;
	d = d < 10 ? "0" + d : d;
	var day = y + "-" + m + "-" + d;
	return day;
  }
  // 获取本月、上月、下月的起始、结束日期
  export function getMonth(type, months) {
	var d = new Date();
	var year = d.getFullYear();
	var month = d.getMonth() + 1;
	if (Math.abs(months) > 12) {
	  months = months % 12;
	}
	if (months != 0) {
	  if (month + months > 12) {
		year++;
		month = (month + months) % 12;
	  } else if (month + months < 1) {
		year--;
		month = 12 + month + months;
	  } else {
		month = month + months;
	  }
	}
	month = month < 10 ? "0" + month : month;
	var date = d.getDate();
	var firstday = year + "-" + month + "-" + "01";
	var lastday = "";
	if (
	  month == "01" ||
	  month == "03" ||
	  month == "05" ||
	  month == "07" ||
	  month == "08" ||
	  month == "10" ||
	  month == "12"
	) {
	  lastday = year + "-" + month + "-" + 31;
	} else if (month == "02") {
	  if (
		(year % 4 == 0 && year % 100 != 0) ||
		(year % 100 == 0 && year % 400 == 0)
	  ) {
		lastday = year + "-" + month + "-" + 29;
	  } else {
		lastday = year + "-" + month + "-" + 28;
	  }
	} else {
	  lastday = year + "-" + month + "-" + 30;
	}
	var day = "";
	if (type == "s") {
	  day = firstday;
	} else {
	  day = lastday;
	}
	return day;
  }
  
